<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/homeIndex' }">
        <i class="el-icon-s-custom"></i> 人事管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>员工请假登记</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>

    <el-row>
      <el-col :span="24" class="col-in-page">
        <!-- 放搜索条件 
        :inline="ture"让表单元素占一行,一行不够会换行-->
        <el-form :inline="true">
          <el-form-item label="员工姓名">
            <el-input v-model="orderParam.empName" placeholder="员工姓名"></el-input>
          </el-form-item>
          <el-form-item label="电话号码">
            <el-input v-model="orderParam.tel" placeholder="电话号码"></el-input>
          </el-form-item>
          <el-form-item label="请假开始时间">
            <el-date-picker v-model="orderParam.startTime" placeholder="请假开始时间"></el-date-picker>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="queryEmployee">查询</el-button>
            <el-button type="primary" icon="el-icon-refresh" @click="clear">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>

      <el-col :span="24" class="col-in-page">
        <!-- 放表格数据 -->
        <el-button type="danger" icon="el-icon-plus" @click="putawayVisible=true">新建登记</el-button>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column fixed prop="empName" label="员工姓名" width="180px"></el-table-column>
          <el-table-column prop="departName" label="部门" width="180px"></el-table-column>
          <el-table-column prop="tel" label="电话" width="180px"></el-table-column>
          <el-table-column prop="days" label="请假天数" width="180px">
            <template slot-scope="scope">{{ scope.row.days }}天</template>
          </el-table-column>
          <el-table-column prop="startTime" label="请假开始时间" width="280px"></el-table-column>
          <el-table-column prop="endTime" label="请假结束时间" width="280px"></el-table-column>
          <el-table-column prop="type" label="请假类型" width="180px">
            <template slot-scope="scope">
              {{ scope.row.status === 0 ? '事假' : scope.row.status === 1 ? '病假'
              : scope.row.status === 2 ? '婚假':scope.row.status === 3 ?'调休':'其他'}}
            </template>
          </el-table-column>
          <el-table-column prop="reviewerName" label="审批人" width="180px">
            <template v-slot:default="scope">{{ getReviewerName(scope.row.reviewerid) }}</template>
          </el-table-column>
          <el-table-column prop="createTime" label="登记时间" width="280px"></el-table-column>
          <el-table-column prop="updateTime" label="审核时间" width="280px"></el-table-column>

          <el-table-column fixed="right" prop="status" label="请假状态" width="120px">
            <template slot-scope="scope">
              <el-tag
                :type="scope.row.status === 0 ? 'danger' : scope.row.status === 1 ? 'success' : 'info'"
              >{{ scope.row.status === 0 ? '待审批' : scope.row.status === 1 ? '已审批' : '已失效' }}</el-tag>
            </template>
          </el-table-column>

          <el-table-column fixed="right" label="操作" width="230px">
            <template slot-scope="scope">
              <el-button
                v-if="scope.row.status === 0"
                type="primary"
                icon="el-icon-edit"
                @click="openLookDialog(scope.row.id)"
              >审批</el-button>
              <!-- 查看和撤回按钮只有在状态为1或2时显示 -->
              <el-button
                v-if="scope.row.status === 1 || scope.row.status === 2"
                icon="el-icon-search"
                @click="openLookDialog1(scope.row.id)"
              >查看</el-button>
              <el-button
                v-if="scope.row.status === 1"
                type="danger"
                @click="resetRest(scope.row.id)"
                icon="el-icon-delete"
              >撤回</el-button>
              <el-button
                v-if="scope.row.status === 2"
                icon="el-icon-delete"
                type="danger"
                @click="delEmpRest(scope.row.id)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          :page-sizes="[3, 5, 10, 20]"
          :page-size="pageSize"
          :current-page="pageNum"
          layout="total, sizes, prev, pager, next"
          @next-click="nextPage"
          @prev-click="prevPage"
          @size-change="sizeChange"
          @current-change="currentPage"
          :total="total"
        ></el-pagination>
      </el-col>
    </el-row>

    <!-- 请假登记的弹框 label-width:是label文字占的宽度-->
    <el-dialog title="请假登记" :visible.sync="putawayVisible">
      <el-form>
        <el-row>
          <el-col :span="12">
            <el-form-item label="员工姓名" label-width="100px" required="">
              <el-select v-model="employee.name" placeholder="请选择" @change="handleEmployeeSelect">
                <el-option
                  v-for="idem in employeeList"
                  :key="idem.name"
                  :label="idem.name"
                  :value="idem.name"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="员工性别" label-width="100px">
              <el-input :value="genderText" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="员工部门" label-width="100px">
              <el-input v-model="employee.departName" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电话号码" label-width="100px">
              <el-input v-model="employee.tel" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="开始时间" label-width="100px" required="">
              <el-date-picker v-model="employeeRest.startTime" type="datetime" placeholder="开始时间"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束时间" label-width="100px" required="">
              <el-date-picker v-model="employeeRest.endTime" type="datetime" placeholder="结束时间"></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="请假类型" label-width="100px" required="">
              <el-select placeholder="请选择类型" v-model="employeeRest.type">
                <el-option label="事假" value="0"></el-option>
                <el-option label="病假" value="1"></el-option>
                <el-option label="婚假" value="2"></el-option>
                <el-option label="调休" value="3"></el-option>
                <el-option label="其他" value="4"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="审核人" label-width="100px" required="">
              <el-select v-model="employeeRest.reviewerid" placeholder="请选择">
                <el-option
                  v-for="idem in reviewerList"
                  :key="idem.id"
                  :label="idem.name"
                  :value="idem.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="请假原因" label-width="100px">
          <el-input
            type="textarea"
            :autosize="{ minRows: 4, maxRows: 10}"
            placeholder="添加备注..."
            v-model="employeeRest.reason"
          ></el-input>
        </el-form-item>
        <p>当前操作人 : {{ operator.name }}</p>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="putawayVisible = false">取 消</el-button>
        <el-button type="primary" @click="addEmployeeRest">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 审批的弹框 label-width:是label文字占的宽度-->
    <el-dialog title="请假审批" :visible.sync="lookVisible">
      <el-form>
        <el-row>
          <el-col :span="12">
            <el-form-item label="员工姓名" label-width="100px">
              <el-input :value="employeeRest1.empName" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="员工性别" label-width="100px">
              <el-input :value="genderText1" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="员工部门" label-width="100px">
              <el-input :value="employeeRest1.departName" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电话号码" label-width="100px">
              <el-input :value="employeeRest1.tel" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="开始时间" label-width="100px">
              <el-input :value="employeeRest1.startTime" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束时间" label-width="100px">
              <el-input :value="employeeRest1.endTime" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="请假类型" label-width="100px">
              <el-input :value="getTypeText(employeeRest1.type)" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="请假天数" label-width="100px">
              <el-input :value="employeeRest1.days" disabled>
                <template #append>天</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="请假原因" label-width="100px">
          <el-input :value="employeeRest1.reason" disabled></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="agreeRest">审核通过</el-button>
        <el-button type="danger" @click="refuseRest">审核驳回</el-button>
      </div>
    </el-dialog>

    <!-- 查看审批详情的弹框 label-width:是label文字占的宽度-->
    <el-dialog title="请假登记详情" :visible.sync="lookVisible1">
      <el-form>
        <el-row>
          <el-col :span="12">
            <el-form-item label="员工姓名" label-width="100px">
              <el-input :value="employeeRest1.empName" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="员工性别" label-width="100px">
              <el-input :value="genderText1" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="员工部门" label-width="100px">
              <el-input :value="employeeRest1.departName" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电话号码" label-width="100px">
              <el-input :value="employeeRest1.tel" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="开始时间" label-width="100px">
              <el-input :value="employeeRest1.startTime" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束时间" label-width="100px">
              <el-input :value="employeeRest1.endTime" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="请假类型" label-width="100px">
              <el-input :value="getTypeText(employeeRest1.type)" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="请假天数" label-width="100px">
              <el-input :value="employeeRest1.days" disabled>
                <template #append>天</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="请假原因" label-width="100px">
              <el-input :value="employeeRest1.reason" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="请假状态" label-width="100px">
              <div
                :class="employeeRest1.status === 1 ? 'green-text' : employeeRest1.status === 2 ? 'gray-text' : ''"
              >{{ getLeaveStatusText(employeeRest1.status) }}</div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="登记人" label-width="100px">
              <el-input :value="employeeRest1.operatorName " disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="登记时间" label-width="100px">
              <el-input :value="employeeRest1.createTime" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="审核人" label-width="100px">
              <el-input :value="employeeRest1.reviewerName" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="审核时间" label-width="100px">
              <el-input :value="employeeRest1.updateTime" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>
    
<script>
export default {
  data() {
    return {
      pageNum: 1,
      pageSize: 5,
      total: 0,
      putawayVisible: false,
      lookVisible: false,
      lookVisible1: false,
      orderParam: {
        empName: "",
        tel: "",
        startTime: ""
      },
      tableData: [],
      employeeList: [],
      employee: {},
      operator: {}, //当前操作人
      employeeRest: {},
      employeeRest1: {},
      reviewerList: [] //所有审批人
    };
  },
  methods: {
    getLeaveStatusText(status) {
      switch (status) {
        case 1:
          return "已审批";
        case 2:
          return "已失效";
        default:
          return "";
      }
    },
    //转换请假类型
    getTypeText(code) {
      switch (code) {
        case 0:
          return "事假";
        case 1:
          return "病假";
        case 2:
          return "婚假";
        case 3:
          return "调休";
        default:
          return "其他";
      }
    },
    clear() {
      this.orderParam.startTime = "";
      this.orderParam.empName = "";
      this.orderParam.tel = "";
    },
    prevPage(val) {
      //点击上一页触发
      this.pageNum = val;
      this.queryEmployee();
    },
    nextPage(val) {
      //点击下一页触发
      this.pageNum = val;
      this.queryEmployee();
    },
    sizeChange(val) {
      //val每一页条数
      this.pageNum = 1;
      this.pageSize = val;
      this.queryEmployee();
    },
    currentPage(val) {
      this.pageNum = val;
      this.queryEmployee();
    },
    queryCondition() {
      thispageNum = 1;
      this.queryEmployee();
    },
    queryEmployee() {
      //带分页参数pageNum,pageSize
      let pageParam = {};
      pageParam.pageNum = this.pageNum;
      pageParam.pageSize = this.pageSize;
      pageParam.param = this.orderParam;
      if (this.orderParam.startTime != "") {
        this.orderParam.startTime = this.$moment(
          this.orderParam.startTime
        ).format("YYYY-MM-DD");
      }
      this.$axios.post("api/employeeRest/queryRest", pageParam).then(res => {
        if (res.data.code == 200) {
          let pageData = res.data.data;
          this.tableData = pageData.list;
          //给分页参数赋值
          this.total = pageData.total;
          this.pageNum = pageData.pageNum;
          this.pageSize = pageData.pageSize;
        }
      });
    },
    addEmployeeRest() {
      //日期转为字符串
      if (this.employeeRest.startTime) {
        this.employeeRest.startTime = this.$moment(
          this.employeeRest.startTime
        ).format("YYYY-MM-DD HH:mm:ss");
      } else {
        this.employeeRest.startTime = "";
      }
      if (this.employeeRest.endTime) {
        this.employeeRest.endTime = this.$moment(
          this.employeeRest.endTime
        ).format("YYYY-MM-DD HH:mm:ss");
      } else {
        this.employeeRest.endTime = "";
      }
      //检查开始时间是否至少提前一天，时间差是否大于24小时（86400000毫秒）
      if (
        new Date(this.employeeRest.startTime).getTime() - new Date().getTime() <
        86400000
      ) {
        this.$message.error("请假开始时间必须至少提前一天");
        return;
      }

      //检查结束时间是否大于开始时间
      if (
        new Date(this.employeeRest.startTime).getTime() >=
        new Date(this.employeeRest.endTime).getTime()
      ) {
        this.$message.error("请假结束时间必须大于开始时间");
        return;
      }

      let employeeRestDTO = {
        employee: this.employee,
        employeeRest: this.employeeRest,
        empid: this.operator.id
      };
      this.$axios
        .post("api/employeeRest/addEmployeeRest", employeeRestDTO)
        .then(res => {
          if (res.data.code == 200) {
            this.$message.success("添加成功!");
            this.queryEmployee();
            this.putawayVisible = false;
          } else {
            this.$message.error(res.data.msg);
          }
        });
    },
    //查所有未离职未休假的员工
    queryNowEmp() {
      this.$axios.get("api/employee/queryNowEmp").then(res => {
        if (res.data.code == 200) {
          this.employeeList = res.data.data;
        }
      });
    },
    //切换员工名字时候触发
    handleEmployeeSelect(value) {
      const selectedEmployee = this.employeeList.find(
        item => item.name === value
      );
      if (selectedEmployee) {
        this.employee = JSON.parse(JSON.stringify(selectedEmployee));
        // 这里增加深拷贝使得 this.employee 和 this.employeeList 中的对应项实际上是两个完全独立的对象
      }
    },
    //查询当前操作人
    queryOperator() {
      this.$axios.get("api/employee/queryOperator").then(res => {
        if (res.data.code == 200) {
          this.operator = res.data.data;
        }
      });
    },
    //查询所有审批人
    queryReviewerList() {
      this.$axios.get("api/employee/queryReviewerList").then(res => {
        if (res.data.code == 200) {
          this.reviewerList = res.data.data;
        }
      });
    },
    //查看展示审批员工信息
    openLookDialog(val) {
      console.log(val);
      this.$axios.get("api/employeeRest/queryRestEmployee/" + val).then(res => {
        if (res.data.code == 200) {
          this.employeeRest1 = res.data.data;
        }
      });
      this.lookVisible = true; // 打开弹窗
    },
    //查看展示员工信息
    openLookDialog1(val) {
      this.$axios.get("api/employeeRest/queryRestEmployee/" + val).then(res => {
        if (res.data.code == 200) {
          this.employeeRest1 = res.data.data;
        }
      });
      this.lookVisible1 = true; // 打开弹窗
    },
    getReviewerName(id) {
      const reviewer = this.reviewerList.find(item => item.id === id);
      return reviewer ? reviewer.name : "";
    },
    //审批通过
    agreeRest() {
      this.$axios
        .post("api/employeeRest/agreesRest", this.employeeRest1)
        .then(res => {
          if (res.data.code == 200) {
            this.$message.success("审批成功!");
            this.queryEmployee();
            this.lookVisible = false;
          } else {
            this.$message.error(res.data.msg);
          }
        });
    },
    //审批驳回
    refuseRest() {
      this.$axios
        .post("api/employeeRest/refuseRest", this.employeeRest1)
        .then(res => {
          if (res.data.code == 200) {
            this.$message.success("驳回成功!");
            this.queryEmployee();
            this.lookVisible = false;
          } else {
            this.$message.error(res.data.msg);
          }
        });
    },
    //删除失效的员工休假
    delEmpRest(val) {
      this.$confirm("确认删除吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$axios.get("api/employeeRest/delEmpRest/" + val).then(res => {
            if (res.data.code == 200) {
              //发删除请求
              this.$message({
                type: "success",
                message: "删除成功!"
              });
              this.queryEmployee();
            } else {
              this.$message.error(res.data.msg);
            }
          });
        })
        .catch(() => {});
    },
    //撤回请假
    resetRest(val) {
      this.$confirm("确认撤回吗?", "提示", {
        confirmButtonText: "继续",
        cancelButtonText: "暂不",
        type: "warning"
      })
        .then(() => {
          this.$axios.get("api/employeeRest/resetRest/" + val).then(res => {
            if (res.data.code == 200) {
              //发删除请求
              this.$message({
                type: "success",
                message: "撤回成功!"
              });
              this.queryEmployee();
            } else {
              this.$message.error(res.data.msg);
            }
          });
        })
        .catch(() => {});
    }
  },
  created() {
    this.queryEmployee();
    this.queryNowEmp();
    this.queryOperator();
    this.queryReviewerList();
  },
  computed: {
    genderText() {
      return this.employee.gender == 0 ? "女" : "男";
    },
    genderText1() {
      return this.employeeRest1.gender == 0 ? "女" : "男";
    }
  }
};
</script>
    
<style scoped>
.green-text {
  color: green;
}
.gray-text {
  color: gray;
}
.el-table {
  margin-top: 20px;
  border-radius: 5px;
  box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
}
.el-form-item {
  margin: 10px 50px;
}
.el-button {
  border-radius: 20px;
  text-align: center; /* 文字居中  */
  border: none;
  color: white; /* 文字颜色白色  */
  cursor: pointer;
  display: inline-block;
  background: linear-gradient(
    to right,
    rgb(176, 119, 218),
    rgb(129, 214, 238)
  ); /* 渐变效果更换为浅紫色到浅蓝色 */
  transition-property: background-color, transform; /* 添加过渡属性 */
  transition-duration: 0.3s; /* 过渡持续时间 */
}

.el-button:hover {
  transform: scale(1.1); /* 按钮放大效果  */
  transition: transform 0.3s ease-in-out;
}
.el-pagination {
  margin-top: 20px; /* 或者你觉得合适的值 */
}
.col-in-page {
  position: relative;
  margin: 1rem;
  padding: 2.5rem;
  background: white;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
  overflow: hidden;
  background: linear-gradient(60deg, #c4dcda 0%, #96e4e1 100%);
}
.el-pagination {
  margin-top: 20px; /* 或者你觉得合适的值 */
}
.form-required:before {
  content: "*";
  color: red;
}
</style>